@extends('layouts.main')

@section('title', '零售合同信息')

@section('content')
<h3>@yield('title')</h3>
<div class="btn-group" role="group">
  	<a href="{{ action('BusinessData\RetailContractIndicatorController@create') }}" class="btn btn-primary">添加</a>
    <a href="{{ action('BusinessData\RetailContractIndicatorController@import') }}" class="btn btn-warning">导入数据</a>
</div>

@if(session('success'))
<div class="alert alert-success" role="alert">{{ session('success') }}</div>
@endif

<div class="clearfix"></div>
<hr>

<form class="form-inline pull-right" method="get" action="{{ action('BusinessData\RetailContractIndicatorController@index') }}">
    
    <div class="form-group">
        <label for="year">年份</label>
        <input type='text' name="year" class="form-control" value="{{ $year }}" />
    </div>

    <div class="form-group">
        <label for="category_contract_id">合同大类</label>
        <select class="form-control" id="category_contract_id" name="category_contract_id">
            <option value="0">请选择合同大类</option>
            @foreach($category_contracts as $category_contract)
            <option value="{{ $category_contract->id }}" @if(request()->category_contract_id == $category_contract->id) selected @endif>{{ $category_contract->name }}</option>
            @endforeach
          </select>
    </div>

    <div class="form-group">
        <label for="payer_name">客户名称</label>
        <input type='text' name="payer_name" class="form-control" value="{{ $payer_name }}" />
    </div>

    <button type="submit" name="search" value="search" class="btn btn-primary">查询</button>

</form>

<div class="clearfix"></div>
<hr>

<table class="table table-bordered table-striped table-hover">
    <tr>
		<th>id</th>
		<th>年度</th>
		<th>客户代码</th>
		<th>客户名称</th>
		<th>合同大类</th>
		<th>指标(元)</th>
		<th>操作</th>
    </tr>
  @forelse($models as $model)
	<tr>
		<td>{{ $model->id }}</td>
		<td>{{ $model->year }}</td>
		<td>{{ $model->payer_code }}</td>
		<td>{{ $model->payer->name }}</td>
		<td>{{ $model->category_contract->name }}</td>
		<td>{{ number_format($model->amount, 2) }}</td>
		<td>
			<a href="{{ action('BusinessData\RetailContractIndicatorController@edit', $model) }}" class="btn btn-warning btn-sm">编辑</a>
			<button name="delete" value="{{ $model->id }}" class="btn btn-danger btn-sm">删除</button>
		</td>
	</tr>
  @empty
  	<tr><td>暂无数据</td></tr>
  @endforelse
</table>
{{ $models->appends([
    'year' => $year,
    'payer_name' => $payer_name,
    'category_contract_id' => $category_contract_id,
    ])->links() }}
<script>
      $(document).ready(function(){
        $("button[name=delete]").click(function(){
            var that = $(this);
            var id = that.attr("value");
            $.post("/business_data/retail_contract_indicator/" + id, {
                '_method': 'DELETE',
                '_token': '{{ csrf_token() }}',
            }, function(data){
                switch(data.status){
                    case 0:
                        that.parent().parent('tr').remove();
                        alert("删除信息成功！");
                        break;
                }
            });
        });
    });
</script>
@endsection